import 'package:cbec_app/components/base/base_image.dart';
import 'package:cbec_app/components/base/base_page.dart';
import 'package:cbec_app/components/common/common_style.dart';
import 'package:cbec_app/components/base/base_nav_bar.dart';
import 'package:cbec_app/components/common/not_found.dart';
import 'package:cbec_app/entity/user_center/bank_card_item_entity.dart';
import 'package:cbec_app/pages/my/controller/bank_ctr.dart';
import 'package:cbec_app/utils/Tools.dart';
import 'package:cbec_app/utils/logger.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

/**
 * 银行卡管理
 */

class BankCard extends StatelessWidget {
  BankController bankCtr = Get.find<BankController>();

  BankCard({super.key});

  @override
  Widget build(BuildContext context) {
    return BasePage(
      title: "银行卡管理",
      iconType: IconType.back,
      bodyColor: Colors.white,
      children: [
        Obx(() => Container(
              width: double.infinity,
              height: ScreenUtil().screenHeight,
              alignment: Alignment.topLeft,
              color: Colors.white,
              child: bankCtr.cardList!.isNotEmpty
                  ? Container(
                      margin: EdgeInsets.all(18.w),
                      child: Tools.getPullRefresh(
                        enablePullDown: true,
                        refreshController: bankCtr.baseRefreshCtr,
                        onRefresh: bankCtr.onPageRefresh,
                        onLoading: bankCtr.onPageLoading,
                        child: ListView.builder(
                            itemCount: bankCtr.cardList?.length,
                            itemBuilder: ((context, index) {
                              logger.d('index $index');
                              final BankCardItemEntity item = bankCtr.cardList![index]!;
                              return BankCardItem(id: item.id!, bankName: item.bankName!, bankAvatarUrl: "default_avatar.png", bankType: item.cardType == 0 ? '个人储蓄卡' : '企业储蓄卡', bankNumber: item.bankCard!);
                            })),
                      ))
                  : const NotFound(txt: "暂无银行卡,请先添加"),
            )),
        Positioned(
            key: GlobalKey(debugLabel: 'Positioned'),
            bottom: MediaQuery.of(context).padding.bottom,
            child: Container(
              padding: EdgeInsets.only(left: 18.w, right: 18.w, bottom: 10.w),
              child: solidButton("添加银行卡", () {
                bankCtr.initAdd();
              }),
            )),
      ],
    );
  }
}

class BankCardItem extends StatelessWidget {
  final String id;
  final String bankName;
  final String bankAvatarUrl;
  final String bankType;
  final String bankNumber;

  const BankCardItem({super.key, required this.bankName, required this.bankAvatarUrl, required this.bankType, required this.bankNumber, required this.id});

  @override
  Widget build(BuildContext context) {
    final BankController bkCtr = Get.find<BankController>();
    return InkWell(
      onTap: () => {},
      child: Container(
        padding: EdgeInsets.fromLTRB(19.w, 23.w, 19.w, 23.w),
        margin: EdgeInsets.only(bottom: 10.w),
        decoration: border_radis(Tools.fromHex("#B85957")),
        child: Column(
          //第一行包含logo、银行名和删除按钮
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                //银行logo
                Row(
                  children: [
                    BaseImage(url: bankAvatarUrl, width: 26, height: 26, borderRadius: BorderRadius.circular(26 / 2)),
                    SizedBox(
                      width: 10.w,
                    ),
                    Text(
                      bankName,
                      style: whiteFont(15),
                    ),
                  ],
                ),
                InkWell(
                  onTap: () => {
                    Tools.showModal(
                        content: "确定要删除该银行卡么?",
                        onCancel: (tag) => {},
                        onConfirm: ((tag) => {
                              bkCtr.delete(id, tag),
                            }))
                  },
                  child: Text(
                    "删除",
                    style: whiteFont(13),
                  ),
                )
              ],
            ),
            Container(
              margin: EdgeInsets.only(left: 35.w),
              child: Text(
                bankType,
                style: whiteFont(13),
              ),
            ),
            SizedBox(
              height: 30.h,
            ),
            //银行卡号
            Container(
              width: 200.w,
              margin: EdgeInsets.only(left: 18.w),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  Text(
                    "****",
                    style: whiteFont(15),
                  ),
                  Text(
                    "****",
                    style: whiteFont(15),
                  ),
                  Text(
                    "****",
                    style: whiteFont(15),
                  ),
                  Text(
                    bankNumber.substring(bankNumber.length - 4),
                    style: whiteFont(15),
                  ),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}
